/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MCSPositioning extends MLBaseClass implements IMLBroadcasterListener {

		private var axisName:String;
		private var mcsHolder:IMCSCollectionController;
		private var distance:Number;
		private var isYAxis:Boolean;

		public function MCSPositioning(mcsHolder:IMCSCollectionController, isYAxis:Boolean) {
			this.mcsHolder=mcsHolder;
			this.distance=distance;
			this.isYAxis=isYAxis;
			axisName=isYAxis ? 'y' : 'x';
			
			mcsHolder.addListener(this);
		}
		
		public function draw():void {
			var mcs:Array=mcsHolder.getMCS();
			var i:uint;
			var loopMC:MovieClip;
			for(i=1;i<mcs.length;i++){
				loopMC=mcs[i];
				loopMC[axisName]=mcs[i-1][axisName]+ (isYAxis ? mcsHolder.getMCHeight(loopMC) : mcsHolder.getMCWidth(loopMC));
			}
		}
		
		public function getSizeByIndex(index:uint):Number {
			var mcs:Array=mcsHolder.getMCS();
			return mcs[index][axisName]+ (isYAxis ? mcsHolder.getMCHeight(mcs[index]) : mcsHolder.getMCWidth(mcs[index]));
		}
		
		public function getSize(index:Number):Number {
			var mcs:Array=mcsHolder.getMCS();
			return mcs[mcs.length-1][axisName]+ (isYAxis ? mcsHolder.getMCHeight(mcs[mcs.length-1]) : mcsHolder.getMCWidth(mcs[mcs.length-1]));
		}
		
//********************************************************************************************
//		events for MCSPositioning
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case mcsHolder:
					switch(ev.event) {
						case mcsHolder.event_MCSSumChanged:
							draw();
						break;
					}
				break;
			}
		}
		
	}
}
